# @Time : 2020/8/28 15:18
# @Author : Fioman 
# @Phone : 13149920693
import cv2 as cv
import numpy as np
import imutils

image = cv.imread("pic/18.png")
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

cv.imshow("Original", image)
cnts = cv.findContours(gray.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv.contourArea, reverse=True)[:5]

cv.drawContours(image, cnts, -1, (0, 255, 0), 2)
cv.imshow("Contours", image)
cv.waitKey(0)

for c in cnts:
    M = cv.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cv.circle(image, (cX, cY), 10, (0, 0, 255), -1)

cv.imshow("Centroids", image)
cv.waitKey(0)

# 计算面积和周长

for index, c in enumerate(cnts):
    area = cv.contourArea(c)
    perimeter = cv.arcLength(c, True)

    print("Contour # {} area = {:.2f},perimeter = {:.2f}".format(index + 1, area, perimeter))

    cv.drawContours(image, [c], -1, (0, 255, 0), 2)
    M = cv.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cv.putText(image, "# {}".format(index + 1), (cX - 20, cY), cv.FONT_HERSHEY_SIMPLEX, 1.25, (255, 255, 255), 4)

cv.imshow("Contours", image)
cv.waitKey(0)

image = cv.imread("pic/18.png")
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

# boundingBox 么有考虑旋转,仅仅就是一个矩形框包围了原来的轮廓.它返回的特征是矩形左上角的坐标以及长宽,它没有旋转角度
clone = image.copy()
cnts = cv.findContours(gray.copy(), cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key=cv.contourArea, reverse=True)[:5]

for c in cnts:
    x, y, w, h = cv.boundingRect(c)
    cv.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv.imshow("Bounding-Boxes", clone)
cv.waitKey(0)

# 最小外接矩形,带旋转角度的,返回的是中心点坐标,宽高w和h.还有旋转角度
clone = image.copy()
for c in cnts:
    box = cv.minAreaRect(c)
    rectPoints = np.int0(cv.boxPoints(box))
    cv.drawContours(clone, [rectPoints], -1, (0, 255, 0), 2)

cv.imshow("MinAreaRect", clone)
cv.waitKey(0)

# 最小外接圆(封闭圆),返回值是圆心和半径
clone = image.copy()
for c in cnts:
    (x, y), radius = cv.minEnclosingCircle(c)
    cv.circle(clone, (int(x), int(y)), int(radius), (0, 255, 0), 2)

cv.imshow("Min-Enclosing-Circles", clone)
cv.waitKey(0)

# 返回拟合椭圆,返回值是椭圆圆心还有就是长轴和短轴的半径值,注意超过四个点的,我们没有办法拟合一个椭圆.
clone = image.copy()
for c in cnts:
    ellipse = cv.fitEllipse(c)
    print("Ellipse = {}".format(ellipse))
    (cx, cy), (longR, shortR), angle = ellipse
    cv.ellipse(clone, (int(cx), int(cy)), (int(longR), int(shortR)), angle, 0, 360, (0, 255, 0), 2)

cv.imshow("Ellipse", clone)
cv.waitKey(0)

image = cv.imread("pic/19.png")
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
clone = image.copy()

cnts = cv.findContours(gray, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)

for index, c in enumerate(cnts):
    M = cv.moments(c)
    cX = int(M["m10"] / M["m00"])
    cY = int(M["m01"] / M["m00"])
    cv.putText(clone, "#{} ({},{})".format(index + 1, cX, cY), (cX - 20, cY), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 4)
    print("# {} : 面积 = {}".format(index + 1, cv.contourArea(c)))
    print("# {} : 周长 = {}".format(index + 1, cv.arcLength(c, True)))
    print("# {} : boundingBox = {}".format(index + 1, cv.boundingRect(c)))
    print("# {} : EnclosingCircle = {}".format(index + 1, cv.minEnclosingCircle(c)))

cv.imshow("Center", clone)
cv.waitKey(0)

area = cv.contourArea(cnts[0])
x, y, w, h = cv.boundingRect(cnts[0])
boundArea = w * h
extend = area / boundArea


area = cv.contourArea(cnts[0])
hull = cv.convexHull(cnts[0])
hullArea = cv.convexHull(hull)
solidity = area / hullArea












